package com.yisoo.ubag.app.service.impl;

import com.yisoo.common.utils.R;
import com.yisoo.ubag.app.entity.CouponEntity;
import com.yisoo.ubag.app.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.app.dao.PayDao;
import com.yisoo.ubag.app.entity.PayEntity;
import com.yisoo.ubag.app.service.PayService;


@Service("payService")
public class PayServiceImpl extends ServiceImpl<PayDao, PayEntity> implements PayService {

    @Autowired
    CouponService couponService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PayEntity> page = this.page(
                new Query<PayEntity>().getPage(params),
                new QueryWrapper<PayEntity>()
        );

        return new PageUtils(page);
    }

//    @Override
//    public R showPay(String uid) {
//        QueryWrapper<PayEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("app_user_id",uid);
//        PayEntity payEntity = baseMapper.selectOne(queryWrapper);
//        if(payEntity == null){
//            //没有这个数据，账户刚注册
//            PayEntity entity = new PayEntity(uid,new BigDecimal(0),"1","普通会员",new BigDecimal(0),1);
//            return R.ok().put("data",entity).put("init",0);
//        }else{
//            //有这个数据
//            return R.ok().put("data",payEntity).put("init",1);
//
//        }
//    }

//    @Override
//    public R initPay(String uid) {
//        QueryWrapper<PayEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("app_user_id",uid);
//        PayEntity payEntity = baseMapper.selectOne(queryWrapper);
//        if(payEntity == null){
//            //没有这个数据，账户刚注册
//            PayEntity entity = new PayEntity(uid,new BigDecimal(20),"1","普通会员",new BigDecimal(0),1);
//            System.out.println(entity);
//            baseMapper.insert(entity);
//
//            return R.ok().put("data",entity).put("init",2);
//        }else{
//            //有这个数据
//            return R.error("优惠券不可用！！！");
//
//        }
//    }

    @Override
    public R getBalance(String uid) {
        DecimalFormat df2 =new DecimalFormat("0.00");
        PayEntity pay = getPayByUidWithNoPayToCreatePay(uid);
        String money =df2.format(pay.getMoney());

        return R.ok().put("balance",money);
    }

    @Override
    public BigDecimal getBalanceData(String uid) {
        return  getPayByUidWithNoPayToCreatePay(uid).getMoney();
    }

    @Override
    public boolean checkNewUser(String uid) {
        QueryWrapper<PayEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_user_id",uid);
        PayEntity payEntity = baseMapper.selectOne(queryWrapper);
        return payEntity == null;
    }

    @Override
    public PayEntity getPayByUidWithNoPayToCreatePay(String uid) {
        if (checkNewUser(uid)){
            PayEntity payEntity = new PayEntity(
                    uid,
                    new BigDecimal("0.00"),
                    "1,2,",
                    "普通会员",
                    new BigDecimal("1.00"),
                    1
            );
             baseMapper.insert(payEntity);
             return payEntity;
        }
        else{
            return baseMapper.selectById(uid);
        }
    }

    @Override
    public R getCoupon(String uid) {
        String coupon = getPayByUidWithNoPayToCreatePay(uid).getCoupon();
        String[] split = coupon.split(",");
        Integer[] ids = new Integer[split.length];
        for (int i=0;i<split.length;i++) {
            if(split[i].length()>0) {
                ids[i] = Integer.valueOf(split[i]);
            }
        }
        List<CouponEntity> couponEntityList = couponService.getCouponByIds(ids);
//        payEntity.getCoupon().
        return R.ok().put("coupons",couponEntityList);
    }

    @Override
    public R useCoupon(String uid, Integer cid) {
        PayEntity payEntity = getPayByUidWithNoPayToCreatePay(uid);
        String coupon = payEntity.getCoupon();
        StringBuffer buffer = new StringBuffer("");
        String[] split = coupon.split(",");
        for (int i=0;i<split.length;i++) {
            if(split[i].equals(String.valueOf(cid))){
                split[i] = "-1";
//                break;
            }else{
                buffer.append(split[i]).append(",");
            }
        }
        payEntity.setCoupon(buffer.toString());
        CouponEntity couponEntity = couponService.getById(cid);

        BigDecimal totalAmount = new BigDecimal(0);
        totalAmount = totalAmount.add(payEntity.getMoney());
        totalAmount = totalAmount.add(couponEntity.getCouponMoney());
        payEntity.setMoney(totalAmount);
        baseMapper.updateById(payEntity);
        return R.ok().put("data",payEntity);
    }


}